package models

import (
	"fmt"
	"gitee.com/captials-team/ubdframe/src/common/utils"
	"gorm.io/gorm/schema"
	"time"
)

type User struct {
	Id int64 `json:"id,omitempty"  gorm:"primaryKey"`
	SqlTimeFields
	SqlSaasTenantFields

	Account  string `json:"account" gorm:"type:varchar(64);index"`
	Password string `json:"password,omitempty" gorm:"type:varchar(32)"`
	Salt     string `json:"salt,omitempty" gorm:"type:varchar(32)"` //密码盐
	Nickname string `json:"nickname" gorm:"type:varchar(64)"`       //昵称
	Sex      int    `json:"sex" gorm:"type:tinyint"`                //性别，0：未知，1：男，2：女
	Avatar   string `json:"avatar" gorm:"type:varchar(256)"`        //头像
	Email    string `json:"email" gorm:"type:varchar(128)"`         //email
	Phone    string `json:"phone" gorm:"type:varchar(32)"`          //手机号

	LastLogin *time.Time `json:"last_login,omitempty"`                      //上次登录时间
	LastIp    string     `json:"last_ip,omitempty" gorm:"type:varchar(20)"` //上次登录ip

	Status int `json:"status,omitempty" gorm:"type:tinyint"`
}

func (u *User) TableName(namer schema.Namer) string {
	return namer.TableName("user")
}

func (u *User) MockCreate() interface{} {
	now := time.Now()
	return &User{
		Nickname:      fmt.Sprintf("sage-%d", utils.TrueScopeRand(1e5, 1e6)),
		Sex:           1,
		Account:       "test",
		Password:      utils.Md5Sum("12345678"),
		Salt:          utils.RandLetterFigureCode(12),
		SqlTimeFields: SqlTimeFields{CreatedAt: &now},
	}
}

func (u *User) MockUpdate() interface{} {
	now := time.Now()
	return &User{
		Id:            utils.TrueScopeRand[int64](1, 1e6),
		Nickname:      fmt.Sprintf("sage-%d", utils.TrueScopeRand[int64](1e5, 1e6)),
		Password:      utils.Md5Sum("12345678"),
		Salt:          utils.RandLetterFigureCode(12),
		SqlTimeFields: SqlTimeFields{UpdatedAt: &now},
	}
}
